﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace System.Linq
{
    public static partial class EnumerableExtension
    {
        public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> onNext)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (onNext == null) throw new ArgumentNullException("onNext");
            foreach (TSource local in source)
            {
                onNext(local);
            }
        }

        public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource, int> onNext)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (onNext == null) throw new ArgumentNullException("onNext");
            int num = 0;
            foreach (TSource local in source)
            {
                onNext(local, num++);
            }
        }

        //public static bool IsEmpty<TSource>(this IEnumerable<TSource> source) {
        //    if (source == null) throw new ArgumentNullException("source");
        //    return !source.Any<TSource>();
        //}

        public static int IndexOf<TSource>(this IEnumerable<TSource> source, Predicate<TSource> pred)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (pred == null) throw new ArgumentNullException("pred");

            int num = 0;
            foreach (TSource local in source)
            {
                if (pred(local)) return num;

                ++num;
            }

            return -1;
        }

        public static bool Contains<TSource>(this IEnumerable<TSource> source, Predicate<TSource> pred)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (pred == null) throw new ArgumentNullException("pred");

            foreach (TSource local in source)
            {
                if (pred(local)) return true;
            }

            return false;
        }

        public static IEnumerable<TSource> OrderByList<TSource, TOrder>(this IEnumerable<TSource> source, IEnumerable<TOrder> order)
        {
            return from s in source
                   from o in order.Select((v, i) => new { obj = v, index = i })
                   where object.Equals(s, o.obj)
                   orderby o.index
                   select s;
        }

        public static IEnumerable<TSource> OrderByList<TSource, TOrder>(this IEnumerable<TSource> source, IEnumerable<TOrder> order, Func<TSource, TOrder, bool> equal)
        {
            var result = from s in source
                         from o in order.Select((v, i) => new { obj = v, index = i })
                         where equal(s, o.obj)
                         orderby o.index
                         select s;

            return result.Union(source.Where(r => !order.Any(k => equal(r, k))));
        }
    }

    public static partial class EnumerableExtension
    {
        #region ForIn

        public static void ForIn<TSource, TAnotherSource>(this IEnumerable<TSource> source,
            IEnumerable<TAnotherSource> collection,
            Func<TSource, TAnotherSource, bool> predicate,
            Action<TSource, TAnotherSource> action)
        {
            foreach (var s in source)
            {
                foreach (var item in collection)
                {
                    if (predicate(s, item))
                    {
                        action(s, item);
                    }
                }
            }
        }

        #endregion ForIn

        #region SearchIn

        public static IEnumerable<TSource> SearchIn<TSource, TAnotherSource>(
                this IEnumerable<TSource> source,
                IEnumerable<TAnotherSource> collection,
                Func<TSource, TAnotherSource, bool> predicate
            )
        {
            foreach (var s in source)
            {
                foreach (var item in collection)
                {
                    if (predicate(s, item))
                    {
                        yield return s;
                    }
                }
            }
        }

        public static IEnumerable<TResult> SearchIn<TSource, TAnotherSource, TResult>(
                this IEnumerable<TSource> source,
                IEnumerable<TAnotherSource> collection,
                Func<TSource, TAnotherSource, bool> predicate,
                Func<TSource, TAnotherSource, TResult> selector
            )
        {
            foreach (var s in source)
            {
                foreach (var item in collection)
                {
                    if (predicate(s, item))
                    {
                        yield return selector(s, item);
                    }
                }
            }
        }

        #endregion SearchIn

        #region UnionWith

        public static void UnionWith<TSource>(this ICollection<TSource> first, IEnumerable<TSource> second)
        {
            var list = first as List<TSource>;
            if (list != null)
            {
                var collection = second.Except(list).ToArray();
                list.AddRange(collection);
            }
            else
            {
                var collection = second.Except(first).ToArray();
                foreach (var item in collection)
                {
                    first.Add(item);
                }
            }
        }

        #endregion UnionWith

        #region ExceptWith

        public static void ExceptWith<TSource>(this ICollection<TSource> first, IEnumerable<TSource> second)
        {
            var list = first as List<TSource>;
            if (list != null)
            {
                var collection = first.Except(second).ToArray();
                list.Clear();
                list.AddRange(collection);
            }
            else
            {
                foreach (var item in second)
                {
                    first.Remove(item);
                }
            }
        }

        #endregion ExceptWith

        public static BindingList<TObject> ToBindingList<TObject>(this IEnumerable<TObject> source)
        {
            return new BindingList<TObject>(source.ToList());
        }

        public static ObservableCollection<TObject> ToObservableCollection<TObject>(this IEnumerable<TObject> source)
        {
            return new ObservableCollection<TObject>(source);
        }
    }
}